import java.util.Scanner;

public class Answer3 {
    //数组中两个字符串的最小距离
    //https://www.nowcoder.com/questionTerminal/2c6a0a8e1d20492f92941400036e0890
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n= in.nextInt();
        String[] str=new String[n];
        String str1=in.next();
        String str2=in.next();

        int prev1=-1;
        int prev2=-1;
        int ret=100000;

        for(int i=0;i<n;i++){
            str[i]=in.next();
            if(str[i].equals(str1)){
                prev1=i;
                if(prev2 !=-1) {
                    ret =  Math.min(ret,prev1 - prev2);
                }
            }else if(str[i].equals(str2)){
                prev2=i;
                if(prev1!=-1){
                    ret =  Math.min(ret,prev2 - prev1);
                }
            }
        }


        System.out.println(ret==100000 ? -1:ret);
    }
}
